package com.ecmcloud.flow.service;

import java.util.Map;

import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstanceQuery;
import org.flowable.task.api.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ecmcloud.common.dto.IndexPageStatistics;
import com.ecmcloud.common.dto.VariableQueryParamDto;

@Service
public class ProcessInstanceService {

	@Autowired
    private TaskService taskService;
    
    @Autowired
    private HistoryService historyService;
    
    @Autowired
    private RuntimeService runtimeService;

    /**
     * 查询统计数量
     * @param userId
     * @return
     */
    public IndexPageStatistics querySimpleData(String userId){
        TaskQuery taskQuery = taskService.createTaskQuery();
        //待办数量
        long pendingNum = taskQuery.taskAssignee(String.valueOf(userId)).count();
        //已完成任务
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        long completedNum = historicActivityInstanceQuery.taskAssignee(String.valueOf(userId)).finished().count();
        IndexPageStatistics indexPageStatistics = IndexPageStatistics.builder().pendingNum(pendingNum).completedNum(completedNum).build();
        return indexPageStatistics;
    }

    /**
     * 查询变量
     * @param paramDto
     * @return
     */
    public Map<String, Object> queryVariables(VariableQueryParamDto paramDto) {
        Map<String, Object> variables = runtimeService.getVariables(paramDto.getExecutionId());
        return variables;
    }
}
